/*
Copyright (c) 2011 ATTi 
All rights reserved.
Author: Michael Shu
Purpose : Tester class for Custom Case Clone function, clone Attachments and Case Comments.
Project : CCIR
Date: 5/25/2011
*/

@isTest

private class CustomCloneFunctionClassTester {
	private static User ccir_user1, ccir_user2;
	
	private static void init(){
		Profile ccir_profile = [select ID, Name from Profile where Name like '%IOG Support - Standard - East%' limit 1];
		UserRole ccir_userrole1 = [select ID, Name from UserRole where Name like '%IOG EAST%' limit 1];
		UserRole ccir_userrole2 = [select ID, Name from UserRole where Name like '%IOG EAST%' limit 1];
		
		User u1 = new User();
		u1.ProfileId = ccir_profile.Id;
		u1.UserRoleId = ccir_userrole1.Id;
		u1.UserName = 'abcd_temp@atti-temp.com';
		u1.LastName = 'Michael Shu Test1';
		u1.Email = 'xyz_1234567890@atti.com';
		u1.Alias = 'abcd1';
		u1.CommunityNickname = 'xyaz_123466';
		u1.TimeZoneSidKey = 'America/Los_Angeles';
		u1.LocaleSidKey = 'en_US';
		u1.EmailEncodingKey = 'UTF-8';
		u1.LanguageLocaleKey = 'en_US';
		Database.Saveresult u_sr1 = Database.insert(u1);
		ccir_user1 = [select id from User where id =: u_sr1.getId()];
		system.assertNotEquals(null, ccir_user1);
		
		User u2 = new User();
		u2.ProfileId = ccir_profile.Id;
		u2.UserRoleId = ccir_userrole2.Id;
		u2.UserName = 'efgh_temp@atti-temp.com';
		u2.LastName = 'Michael Shu Test2';
		u2.Email = 'xyz_1234567890@atti.com';
		u2.Alias = 'abcd2';
		u2.CommunityNickname = 'xyaz_123455';
		u2.TimeZoneSidKey = 'America/Los_Angeles';
		u2.LocaleSidKey = 'en_US';
		u2.EmailEncodingKey = 'UTF-8';
		u2.LanguageLocaleKey = 'en_US';
		Database.Saveresult u_sr2 = Database.insert(u2);
		ccir_user2 = [select id from User where id =: u_sr2.getId()];
		system.assertNotEquals(null, ccir_user2);
	}
	
	static testMethod void testCCIRCaseClonewoRelatedList() {	
		init();				                                                             
		Case c = new Case();
		c.Subject = 'testCCIRCaseClone';
		c.Origin = 'Phone';
		c.Status = 'In Progress';
		c.CSS_CRF_RequestType__c = 'Change Task';
		c.CSS_ProductFamily__c = 'Product1;Product2';
		c.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Case', 'IOG Incident');
		Database.Saveresult c_sr;
		System.runAs(ccir_user1){
			c_sr = Database.insert(c);
		}
		
		ID new_csid;
		System.runAs(ccir_user1){
			new_csid = CustomCloneFunctionClass.CCIRCaseClone(c_sr.getId());
		}		

		Case cloned_cs = [select Subject, Origin, OwnerId, Status, CSS_CRF_RequestType__c, CSS_ProductFamily__c
		                         , (select CommentBody, IsPublished from casecomments) 
		                         , (select ContentType, Name from attachments)
		                  from Case where id =: new_csid];

		System.assertEquals(c.Subject, cloned_cs.Subject);
		System.assertEquals('Open', cloned_cs.Status);
		System.assertEquals('Not Defined', cloned_cs.CSS_CRF_RequestType__c);
		System.assertEquals(c.CSS_ProductFamily__c, cloned_cs.CSS_ProductFamily__c);
		System.assertEquals(ccir_user1.Id, cloned_cs.OwnerId);				              		
	}

	static testMethod void testCCIRCaseClonewithRelatedList() {	
		init();				                                                             
		Case c = new Case();
		c.Subject = 'testCCIRCaseClone';
		c.Origin = 'Phone';
		c.Status = 'In Progress';
		c.CSS_CRF_RequestType__c = 'Change Task';
		c.CSS_ProductFamily__c = 'Product1;Product2';
		c.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Case',  'IOG Incident');
		Database.Saveresult c_sr;
		System.runAs(ccir_user1){
			c_sr = Database.insert(c);
		}
				                  		
		CaseComment cc = new CaseComment();
		cc.ParentId = c_sr.getId();
		cc.CommentBody = 'test comment';
		cc.IsPublished = false;
		System.runAs(ccir_user1){
			insert cc;
		}
		
		Attachment attach = new Attachment();
		attach.ParentId = c_sr.getId();
		attach.Body = Blob.valueOf('Test attachment');
		attach.ContentType = 'text/plain';
		attach.Name = 'test file name';
		System.runAs(ccir_user1){
			insert attach;
		}
				
		ID new_csid;
		System.runAs(ccir_user1){
			new_csid = CustomCloneFunctionClass.CCIRCaseClone(c_sr.getId());
		}
		Case cloned_cs = [select Subject, Origin, OwnerId, Status, CSS_CRF_RequestType__c, CSS_ProductFamily__c
		                         , (select CommentBody, IsPublished from casecomments) 
		                         , (select ContentType, Name from attachments)
		                  from Case where id =: new_csid];
		System.assertEquals(c.Subject, cloned_cs.Subject);
		System.assertEquals('Open', cloned_cs.Status);
		System.assertEquals('Not Defined', cloned_cs.CSS_CRF_RequestType__c);
		System.assertEquals(c.CSS_ProductFamily__c, cloned_cs.CSS_ProductFamily__c);
		System.assertEquals(ccir_user1.Id, cloned_cs.OwnerId);
		
		System.assertEquals(1 , cloned_cs.casecomments.size());
		System.assertEquals(cc.CommentBody , cloned_cs.casecomments[0].CommentBody);
		System.assertEquals(cc.IsPublished , cloned_cs.casecomments[0].IsPublished);
		
		System.assertEquals(1 , cloned_cs.attachments.size());
		System.assertEquals(attach.ContentType , cloned_cs.attachments[0].ContentType);
		System.assertEquals(attach.Name , cloned_cs.attachments[0].Name);
		
	}

}